<?php
/**
 * 
 * @author tangbl
 *
 */
class ProductAction extends CommonAction{
	
	/**
	 * 产品详情
	 */
	public function detail(){
		
		$comment  = $this->_get('comment','',0);
		
		$this->comment();
		//$this->trade();
		
		$id      = $this->_get('id','intval',0);
		$product = D("Product");
		
		$data    = $product->find($id);
		$this->assign("detail",$data);
		
		/* //相关产品
		$map['kind'] = $data['kind'];
		$row = $product->where($map)->limit("6")->select();
		$this->assign("row",$row);
		
		if($data['cateid']){
			$cate = M("Category")->find($data['cateid']);
			$this->assign("cateid",$data['cateid']);
			$this->assign("cate",$cate);
		} */
		
		$this->display();
	}
	
	/**
	 * 加载评论
	 */
	public function comment(){
		
		$id      = $this->_get('id','intval',0);
		$param   = "id=".$id;
		$url     = "Product/comment";
		$target  = "detail_comment";
		$pagesid = "page_comment";
		$model   = M("Comment");
		
		$map['pid']      = $id ;
		$map['ischcked'] = 1 ;
		
		import("ORG.Util.AjaxPage");
		$nowPage   = $this->_get("p","intval",1);
		$pageNum   = 5 ;
		$count     = $model->where($map)->count();
		$star      = $model->where($map)->sum("star");
		$Page      = new AjaxPage($count,$pageNum, $param,$url,$target,$pagesid);
		
		$list      = $model->where($map)->order("addtime desc")//->field("content",true)
					->limit((intval($nowPage)-1)*$pageNum,$pageNum)->select();
		
		$data["page"]  = $Page->show();
		$data['list']  = $list;
		$data['total'] = $count;
		$data['star']  = $star;
		$data['score'] = number_format($star/$count,1);
		$this->assign("comment",$data);
		
		if(IS_AJAX){
			$this->display("comment");
		}else{
			return true;
		}
	}
	
	/**
	 * 加载交易记录
	 */
	public function trade($first=true){
		
		$id      = $this->_get('id','intval',0);
		$param   = "id=".$id;
		$url     = "Product/trade";
		$target  = "detail_trade";
		$pagesid = "page_trade";
		$model   = D("OrderDetailMemInfoView");
		
		$map['pid'] = $id ;
		
		import("ORG.Util.AjaxPage");
		$nowPage   = $this->_get("p","intval",1);
		$pageNum   = 5 ;
		$count     = $model->where($map)->count();
		$total     = $model->where($map)->sum("amount");
		$Page      = new AjaxPage($count,$pageNum, $param,$url,$target,$pagesid);
		
		$list      = $model->where($map)->order("addtime desc")//->field("content",true)
					->limit((intval($nowPage)-1)*$pageNum,$pageNum)->select();
		
		$data["page"]  = $Page->show();
		$data['list']  = $list;
		$data['total'] = $total;
		$this->assign("trade",$data);
		if(IS_AJAX){
			$this->display("trade");
		}else{
			return true;
		}
	}
	
	/**
	 * 搜索商品
	 */
	public function index(){
		
		$lang = LANG_SET;

		$m['keywords'] = $this->_param("keywords");
		$m['cateid']   = $this->_param("cateid","intval",0);
		$m['brandid']  = $this->_param("brandid","intval",0);
		$m['kindid']   = $this->_param("kindid","intval",0);
		$m['originid'] = $this->_param("originid","intval",0);
		$m['gtprice']  = $this->_param("gtprice","intval",0);
		$m['ltprice']  = $this->_param("ltprice","intval",0);
		$m['orderby']  = $this->_param("orderby","","addtime desc");
		$m['attr']     = $this->_param("attr","",0);
		
		if($m['brandid']){
			$map['brandid'] = $m['brandid'] ;
		}
		
		if($m['kindid']){
		    $kindids = explode(",", fatherFindChildsArr($m['kindid'],"Dict_kind"));
		  	$map['kind'] = array('in',$kindids);
		}
		
		if($m['originid']){
			$map['origin'] = $m['originid'] ;
		}
		
		if($m['gtprice']){
			$map['sprice'] = array("egt",$m['gtprice']) ;
			if($m['ltprice']){
				$map['sprice'] = array('between',array($m['gtprice'],$m['ltprice'])) ;
			}
		}
		
		if($m['attr']){
			$map['status'] = $m['attr'] ;
		}
		
		if($m['keywords']){
			$map['title'] = array("like","%".$m['keywords']."%");
		}
		$map['isdel'] = 2;
		$map['type']  = 1;

		import('ORG.Util.Page');
		$model  = D("Product");
		$pnum   = 12 ;
		$count  = $model->where($map)->count();
		$Page   = new Page($count , $pnum);
		$show   = $Page->show();
		
		$data = $model->where($map)->order($m['orderby'])
				->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//获取产品品牌
		$Brands = F("Brands");
		$this->assign("brands",$Brands);
		
		//获取产品种类
		$kind = getDictByKey("Dict_kind",$lang);
		$this->assign("kindlists",$kind["array"]);
		
		//获取产品产地
		$import = getDictByKey("Dict_importadd",$lang);
		$china  = getDictByKey("Dict_chinaadd",$lang);
		$origin = $import['array']+$china['array'];
		$this->assign("originlists",$origin);
		
		$this->assign('page',$show);
		$this->assign("data",$data);
		$this->assign("m",$m);
		$this->assign("empty","没有找到对应的绿叶商品");
		$this->display();
	}
	
	/**
	 * 进口鲜果
	 */
	public function product_import(){
		
		$lang = LANG_SET;

		$m['keywords'] = $this->_param("keywords");
		$m['cateid']   = $this->_param("cateid","intval",0);
		$m['brandid']  = $this->_param("brandid","intval",0);
		$m['kindid']   = $this->_param("kindid","intval",0);
		$m['originid'] = $this->_param("originid","intval",0);
		$m['gtprice']  = $this->_param("gtprice","intval",0);
		$m['ltprice']  = $this->_param("ltprice","intval",0);
		$m['orderby']  = $this->_param("orderby","","addtime desc");
		$m['attr']     = $this->_param("attr","",0);
		
		if($m['brandid']){
			$map['brandid'] = $m['brandid'] ;
		}
		
		if($m['kindid']){
		    $kindids = explode(",", fatherFindChildsArr($m['kindid'],"Dict_kind"));
		  	$map['kind'] = array('in',$kindids);
		}
		
		if($m['originid']){
			$map['origin'] = $m['originid'] ;
		}
		
		if($m['gtprice']){
			$map['sprice'] = array("egt",$m['gtprice']) ;
			if($m['ltprice']){
				$map['sprice'] = array('between',array($m['gtprice'],$m['ltprice'])) ;
			}
		}
		
		if($m['attr']){
			$map['status'] = $m['attr'] ;
		}
		
		if($m['cateid']){
			$map['cateid'] = $m['cateid'] ;
		}
		
		if($m['keywords']){
			$map['title'] = array("like","%".$m['keywords']."%");
		}
		$map['isdel'] = 2;
		$map['type']  = 1;

		import('ORG.Util.Page');
		$model  = D("Product");
		$pnum   = 12 ;
		$count  = $model->where($map)->count();
		$Page   = new Page($count , $pnum);
		$show   = $Page->show();
		
		$data = $model->where($map)->order($m['orderby'])
				->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//获取产品品牌
		$Brands = F("Brands");
		$this->assign("brands",$Brands);
		
		//获取产品种类
		$kind = getDictByKey("Dict_kind",$lang);
		$this->assign("kindlists",$kind["array"]);
		
		//获取产品产地
		$origin = getDictByKey("Dict_importadd",$lang);
		$this->assign("originlists",$origin["array"]);
		
		$this->assign('page',$show);
		$this->assign("data",$data);
		$this->assign("m",$m);
		$this->assign("empty","没有找到对应的绿叶商品");
		
		$this->display();
	}
	
	/**
	 * 国产优果
	 */
	public function product_china(){
		
		$lang = LANG_SET;
		
		$m['keywords'] = $this->_param("keywords");
		$m['cateid']   = $this->_param("cateid","intval",0);
		$m['brandid']  = $this->_param("brandid","intval",0);
		$m['kindid']   = $this->_param("kindid","intval",0);
		$m['originid'] = $this->_param("originid","intval",0);
		$m['gtprice']  = $this->_param("gtprice","intval",0);
		$m['ltprice']  = $this->_param("ltprice","intval",0);
		$m['orderby']  = $this->_param("orderby","","addtime desc");
		$m['attr']     = $this->_param("attr","",0);
		
		if($m['brandid']){
			$map['brandid'] = $m['brandid'] ;
			//$cateids = explode(",", fatherFindChilds($map['brandid']));
			//$map['brandid'] = array('in',$cateids);
		}
		
		if($m['kindid']){
		    $kindids = explode(",", fatherFindChildsArr($m['kindid'],"Dict_kind"));
		  	$map['kind'] = array('in',$kindids);
		}
		
		if($m['originid']){
			$map['origin'] = $m['originid'] ;
			//$cateids = explode(",", fatherFindChilds($map['origin']));
			//$map['origin'] = array('in',$cateids);
		}
		
		if($m['gtprice']){
			$map['sprice'] = array("egt",$m['gtprice']) ;
			if($m['ltprice']){
				$map['sprice'] = array('between',array($m['gtprice'],$m['ltprice'])) ;
			}
		}
		
		if($m['attr']){
			$map['status'] = $m['attr'] ;
		}
		
		if($m['cateid']){
			$map['cateid'] = $m['cateid'] ;
		}
		
		if($m['keywords']){
			$map['title'] = array("like","%".$m['keywords']."%");
		}
		$map['isdel'] = 2;
		$map['type']  = 1;
		
		import('ORG.Util.Page');
		$model  = D("Product");
		$pnum   = 12 ;
		$count  = $model->where($map)->count();
		$Page   = new Page($count , $pnum);
		$show   = $Page->show();
		
		$data = $model->where($map)->order($m['orderby'])
				->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//获取产品品牌
		$Brands = F("Brands");
		$this->assign("brands",$Brands);
		
		//获取产品种类
		$kind = getDictByKey("Dict_kind",$lang);
		$this->assign("kindlists",$kind["array"]);
		
		//获取产品产地
		$origin = getDictByKey("Dict_chinaadd",$lang);
		$this->assign("originlists",$origin["array"]);
		
		$this->assign('page',$show);
		$this->assign("data",$data);
		$this->assign("m",$m);
		$this->assign("empty","没有找到对应的绿叶商品");
		$this->display();
	}
	
	/**
	 * 礼品专区
	 */
	public function product_gift(){
		
		$lang = LANG_SET;
		
		$m['cateid']   = $this->_param("cateid","intval",0);
		$m['suitcwd']  = $this->_param("suitcwd","intval",0);
		$m['suitoca']  = $this->_param("suitoca","intval",0);
		$m['gtprice']  = $this->_param("gtprice","intval",0);
		$m['ltprice']  = $this->_param("ltprice","intval",0);
		$m['orderby']  = $this->_param("orderby","","addtime desc");
		$m['attr']     = $this->_param("attr","",0);
		
		if($m['suitcwd']){
			$map['suitcwd'] = $m['suitcwd'] ;
		}
		
		if($m['suitoca']){
			$map['suitoca'] = $m['suitoca'] ;
		}
		
		if($m['gtprice']){
			$map['sprice'] = array("egt",$m['gtprice']) ;
			if($m['ltprice']){
				$map['sprice'] = array('between',array($m['gtprice'],$m['ltprice'])) ;
			}
		}
		
		if($m['attr']){
			$map['status'] = $m['attr'] ;
		}
		
		if($m['cateid']){
			$map['cateid'] = $m['cateid'] ;
		}
		
		if($m['keywords']){
			$map['title'] = array("like","%".$m['keywords']."%");
		}
		$map['isdel'] = 2;
		$map['type']  = 1;
		
		import('ORG.Util.Page');
		$model  = D("Product");
		$pnum   = 12 ;
		$count  = $model->where($map)->count();
		$Page   = new Page($count , $pnum);
		$show   = $Page->show();
		
		$data = $model->where($map)->order($m['orderby'])
				->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//获取适合人群
		$suitcwd = getDictByKey("Dict_suitcwd",$lang);
		$this->assign("suitcwdlists",$suitcwd["array"]);
		
		//获取适合场合
		$suitoca = getDictByKey("Dict_suitoca",$lang);
		$this->assign("suitocalists",$suitoca["array"]);
		
		$this->assign('page',$show);
		$this->assign("data",$data);
		$this->assign("m",$m);
		$this->assign("empty","没有找到对应的绿叶商品");
		$this->display();
	}
	
	/**
	 * 水果周边
	 */
	public function product_area(){
	
		$lang = LANG_SET;
		
		$m['keywords'] = $this->_param("keywords");
		$m['cateid']   = $this->_param("cateid","intval",0);
		$m['gtprice']  = $this->_param("gtprice","intval",0);
		$m['ltprice']  = $this->_param("ltprice","intval",0);
		$m['orderby']  = $this->_param("orderby","","addtime desc");
		$m['attr']     = $this->_param("attr","",0);
		
		if($m['gtprice']){
			$map['sprice'] = array("egt",$m['gtprice']) ;
			if($m['ltprice']){
				$map['sprice'] = array('between',array($m['gtprice'],$m['ltprice'])) ;
			}
		}
		
		if($m['attr']){
			$map['status'] = $m['attr'] ;
		}
		
		if($m['cateid']){
			$map['cateid'] = $m['cateid'] ;
		}
		
		if($m['keywords']){
			$map['title'] = array("like","%".$m['keywords']."%");
		}
		$map['isdel'] = 2;
		$map['type']  = 1;
		
		import('ORG.Util.Page');
		$model  = D("Product");
		$pnum   = 12 ;
		$count  = $model->where($map)->count();
		$Page   = new Page($count , $pnum);
		$show   = $Page->show();
		
		$data = $model->where($map)->order($m['orderby'])
				->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('page',$show);
		$this->assign("data",$data);
		$this->assign("m",$m);
		$this->assign("empty","没有找到对应的绿叶商品");
		$this->display();
	}
	
	/**
	 * 积分商品列表
	 */
	public function jifen(){
		$user  = getMember();
		$map['cmodule'] = "integral";
		
		import('ORG.Util.Page');
		$model  = D("Integral");
		$count  = $model->where($map)->count();
		$Page   = new Page($count,12);
		$show   = $Page->show();
		
		$data = $model->where($map)->order($m['orderby'])
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//获取最近浏览过的商品
		/* $cat['userid'] = $user['id'];
		$collect = D("MemberCollectView")->where($cat)->order("id desc")->limit("4")->select();
		$this->assign("collect",$collect); */
		
		//获取兑换人数
		$integral = M("Integrallog");
		foreach($data as $k=>$v){
			$map['pid'] = $v['id'];
			$list = $integral->where($map)->select();
			
			$data[$k]['num'] = count($list);
		}
		
		$this->assign("data",$data);
		$this->display();
	}
	
	//积分商品详情
	public function jifendetail(){
		$id = $this->_param("id");
		
		$model  = D("Integral");
		$data   = $model->find($id);
		
		$intelog    = M("Integrallog");
		$map['pid'] = $data['id'];
		$list       = $intelog->where($map)->select();
		
		$data['lognum'] = count($list);
		
		$this->assign("detail",$data);
		$this->display();
	}
	
	//积分兑换商品操作
	public function jifen_deal_bak(){
		$id     = $this->_param("id");
		$num    = $this->_param("num");
		$user   = getMember();
		$log    = M("Integrallog");
		
		$model  = D("Integral");
		$data   = $model->find($id);
		
		$jifen  = $num*$data['integration'];  //所需积分
		
		//兑换条件
		$member          = M("Member")->find($user['id']);        //会员详情
		$map['username'] = $member['mobile'];
		$vipcard         = M("Actvipcardlog")->where($map)->find();   //会员卡详情
		
		if($vipcard['csm_credits']>$jifen){
			//判断是否存在记录，存在就只加数量
			$car['pid']    = $id;
			$car['userid'] = $user['id'];
			$view = $log->where($car)->select();
			
			if(count($view)==0){
				//添加兑换记录
				$list['solevar']  = create_id();
				$list['pid']      = $id;
				$list['num']      = $num;
				$list['userid']   = $user['id'];
				$list['addtime']  = time();
				$list['integral'] = $data['integration'];
				
				$log->add($list);
			}else{
				$log->where($car)->setInc("num",$num);
			}
			
			$model->where("id=".$id)->setInc("amount"); //增加积分商品兑换人数
			
			//更新会员会员卡积分
			$cat['csm_credits'] = $vipcard['csm_credits']-$jifen;  //积分计算
			$test = M("Actvipcardlog")->where($map)->save($cat);
			
			//更新会员卡积分消费记录
			$arr['logid']   =  $vipcard['id'];
			$arr['credit']  =  $jifen;
			$arr['act']     =  "minus";
			$arr['remark']  =  "积分商城兑换商品消费积分：".$jifen;
			$arr['addtime'] =  time();
			M("ActvipcardCredit")->add($arr);
			
			$this->assign("jumpUrl",U('Product/jifen'));
			$this->success("商品兑换成功~！");
			
		}else{
			$this->error("亲，您的积分不足~！");
		}
	}
	
	public function jifen_deal(){
		$userid = session(C('USER_AUTH_KEY'));
		$model  = D("Integral");
		
		$data = $model->dealjifen($userid);
		
		if($data['status']==1){
			$this->success($data['info'],U('Product/jifen'));
		}else{
			$this->error($data['info']);
		}
	} 
	
	public function cate(){
		//获取产品种类
		$lang = getLang();
		$suitoca = getDictByKey("Dict_suitoca",$lang);
		$this->assign("suitoca",$suitoca["array"]);
	
		//获取产品品牌
		$Brands = F("Brands");
		$this->assign("brands",$Brands);
	
		$this->display();
	}
}
